package com.yonyou.handler;

import com.yonyou.entity.ResultEntity;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by WangXin on 2017/5/23.
 */
public class XmlHandler {

    public void BuildXMLDoc(String excelPath,String xmlPath) throws IOException {
        // 创建根节点 并设置它的属性 ;
        Element root = new Element("SQLResults");
        // 将根节点添加到文档中；
        Document Doc = new Document(root);

        InitEntity initEntity = new InitEntity();
        List<ResultEntity> resultEntityList = initEntity.initResultEntity(excelPath);

        for (int i=0;i<resultEntityList.size();i++) {
            Element element0 = new Element("ROW");
            element0.addContent(new Element("Index").setText(String.valueOf(resultEntityList.get(i).getIndex())));
            element0.addContent(new Element("Pk_contractconfirm").setText(String.valueOf(resultEntityList.get(i).getPk_contractconfirm())));
            element0.addContent(new Element("Pk_corp").setText(String.valueOf(resultEntityList.get(i).getPk_corp())));
            element0.addContent(new Element("Vbillno").setText(String.valueOf(resultEntityList.get(i).getVbillno())));
            element0.addContent(new Element("Dutyname").setText(String.valueOf(resultEntityList.get(i).getDutyname())));
            element0.addContent(new Element("Contracttype").setText(String.valueOf(resultEntityList.get(i).getContracttype())));
            element0.addContent(new Element("Contractconfirmmny").setText(String.valueOf(resultEntityList.get(i).getContractconfirmmny())));
            element0.addContent(new Element("Dbilldate").setText(String.valueOf(resultEntityList.get(i).getDbilldate())));
            element0.addContent(new Element("Pk_billtype").setText(String.valueOf(resultEntityList.get(i).getPk_billtype())));
            element0.addContent(new Element("Vbillstatus").setText(String.valueOf(resultEntityList.get(i).getVbillstatus())));
            element0.addContent(new Element("Customsubprojectno").setText(String.valueOf(resultEntityList.get(i).getCustomsubprojectno())));
            element0.addContent(new Element("Acceptorg").setText(String.valueOf(resultEntityList.get(i).getAcceptorg())));
            element0.addContent(new Element("Acceptdept").setText(String.valueOf(resultEntityList.get(i).getAcceptdept())));
            element0.addContent(new Element("Starttime").setText(String.valueOf(resultEntityList.get(i).getStarttime())));
            element0.addContent(new Element("Endtime").setText(String.valueOf(resultEntityList.get(i).getEndtime())));
            element0.addContent(new Element("Voperatorid").setText(String.valueOf(resultEntityList.get(i).getVoperatorid())));
            element0.addContent(new Element("Dmakedat").setText(String.valueOf(resultEntityList.get(i).getDmakedat())));
            element0.addContent(new Element("Vmodifierid").setText(String.valueOf(resultEntityList.get(i).getVmodifierid())));
            element0.addContent(new Element("Dmodifydate").setText(String.valueOf(resultEntityList.get(i).getDmodifydate())));
            element0.addContent(new Element("Vapproveid").setText(String.valueOf(resultEntityList.get(i).getVapproveid())));
            element0.addContent(new Element("Dapprovedate").setText(String.valueOf(resultEntityList.get(i).getDapprovedate())));
            element0.addContent(new Element("Vapprovenote").setText(String.valueOf(resultEntityList.get(i).getVapprovenote())));
            element0.addContent(new Element("Vmemo").setText(String.valueOf(resultEntityList.get(i).getVmemo())));
            element0.addContent(new Element("Def1").setText(String.valueOf(resultEntityList.get(i).getDef1())));
            element0.addContent(new Element("Def2").setText(String.valueOf(resultEntityList.get(i).getDef2())));
            element0.addContent(new Element("Def3").setText(String.valueOf(resultEntityList.get(i).getDef3())));
            element0.addContent(new Element("Def4").setText(String.valueOf(resultEntityList.get(i).getDef4())));
            element0.addContent(new Element("Def5").setText(String.valueOf(resultEntityList.get(i).getDef5())));
            element0.addContent(new Element("Ts").setText(String.valueOf(resultEntityList.get(i).getTs())));
            element0.addContent(new Element("Dr").setText(String.valueOf(resultEntityList.get(i).getDr())));
            element0.addContent(new Element("Isdiv").setText(String.valueOf(resultEntityList.get(i).getIsdiv())));

            root.addContent(element0);
        }
        // 输出 books.xml 文件；
        // 使xml文件 缩进效果
        Format format = Format.getPrettyFormat();
        XMLOutputter XMLOut = new XMLOutputter(format);
        XMLOut.output(Doc,new FileOutputStream(xmlPath));
    }

}
